﻿using System;
using System.Collections.Generic;
using System.Text;
using Ebada.Platform.Model;
using Ebada.Client.Platform;
using Ebada.UI.Base;
using System.Windows.Forms;
using System.Drawing;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 接口类实现
    /// </summary>
    class CVehicleDispatch : InVehicleDispatch
    {
        /// <summary>
        /// 出车标记
        /// </summary>
        /// <param name="VehicleDispatchID"> 派车单号</param>
        /// <returns>成功返回true否则返回false</returns>
        public bool OutVehiclesSign(string VehicleDispatchID)
        {
            bool Result = true;
            try
            {
                VehicleDispatch tempvdh = MainHelper.PlatformSqlMap.GetOneByKey<VehicleDispatch>(VehicleDispatchID);
                if (tempvdh != null)
                {
                    Vehicles tempvh = MainHelper.PlatformSqlMap.GetOneByKey<Vehicles>(tempvdh.VehicleID);

                    if (tempvh != null)
                    {
                        tempvdh.DispatchingTime = DateTime.Now;
                        //如果实际出车时间比预计出车时间晚，则修改预计回车时间为实际出车时间加预计用车天数
                        if (tempvdh.DispatchingTime.Date.CompareTo(tempvdh.ExpectDispatchingTime.Date) > 0)
                        {
                            tempvdh.ExpectReturnTime.AddDays(tempvdh.UsingDays);
                        }
                        if (tempvh.VehicleImage == null)
                        {
                            tempvh.VehicleImage = new byte[0];
                        }
                        MainHelper.PlatformSqlMap.Update<VehicleDispatch>(tempvdh);

                        tempvh.VehicleState = "出车";
                        MainHelper.PlatformSqlMap.Update<Vehicles>(tempvh);
                    }
                    else
                    {
                        Result = false;
                    }
                }
                else
                {
                    Result = false;
                }
            }
            catch (Exception)
            {
                Result = false;
                throw;
            }

            return Result;
        }

        /// <summary>
        /// 返回当天派车列表
        /// </summary>
        /// <param name="OrgID">单位编号</param>
        public void ReturnVehicleDispatch(string OrgCode)
        {
            try
            {
                string sqlwher = " where UsingOrgCode='" + OrgCode + "' and   CONVERT(varchar(12) , DispatchingTime, 112 ) =CONVERT(varchar(12) , getdate(), 112 ) ";
                UCVehicleDispatch_Right ucdr = new UCVehicleDispatch_Right();
                ucdr.RefreshBySql(sqlwher);
                ucdr.HideBar();
                showControl(ucdr, "当日派车信息");
            }
            catch (Exception ew)
            {
                MessageBox.Show("显示当日派车信息错误！" + ew.Message);
            }

           
        }
        /// <summary>
        /// 返回车辆信息
        /// </summary>
        /// <param name="VehicleDispatchID">派车单号</param>
        public void  ReturnVehicle(string VehicleDispatchID)
        {
             VehicleDispatch tempvdh = MainHelper.PlatformSqlMap.GetOneByKey<VehicleDispatch>(VehicleDispatchID);
             if (tempvdh != null)
             {
                 string sqlwhere = " where VehicleID='" + tempvdh.VehicleID + "'";
                 UCVehicles uch = new UCVehicles();
                 uch.RefreshDataBysql(sqlwhere);
                 uch.HideBar();
                 showControl(uch,"车辆信息");
             }
             else
             {
                 MessageBox.Show("派车单号错误！");
             }
        }
        private FormBase showControl(UserControl uc,string Title)
        {
            FormBase dlg = new FormBase();
            Size newsize = new Size(500, 400);
            dlg.Size = newsize;

            dlg.Text = Title;
            dlg.Controls.Add(uc);
            uc.Dock = DockStyle.Fill;
            dlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            dlg.Show();
            return dlg;
        }
    }
}
